package net.orange.framework.validator;

import java.util.Collection;

import net.orange.architecture.process.ProcessFactory;
import net.orange.architecture.validator.IObjectValidator;
import net.orange.framework.logic.IUserProcess;
import net.orange.framework.model.Department;
import net.orange.framework.model.User;

/**
 * Validation : If the department has reference by user, so It can't be
 * deleted.
 */

public class DepartmentHasUserValidator extends FrameworkValidator implements
		IObjectValidator {

	/**
	 * The target department.
	 */
	private Department department;

	/**
	 * Disabled the default contractor.
	 */
	@SuppressWarnings("unused")
	private DepartmentHasUserValidator() throws Exception  {

	}

	/**
	 * Constructor with department object to initialize the validation.
	 * 
	 * @param department
	 *            The target department.
	 */
	public DepartmentHasUserValidator(Department department) throws Exception  {
		this.department = department;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.orange.architecture.validator.ObjectValidator#check()
	 */
	public boolean check() throws Exception {
		IUserProcess process = (IUserProcess) ProcessFactory.get(User.class);
		Collection collection = process.find(department);
		return (collection == null || collection.size() <= 0);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.orange.architecture.validator.ObjectValidator#getMessage()
	 */
	public String getMessage() {
		return department.getDept_name() + message.get("error2_dept_06");
	}
}
